package com.banciyuan.bcywebview.biz.detail.noveldetail.holder;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.regex.MatchFilterImpl;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2017/3/2.
 */

public class ContentHolder extends RecyclerView.ViewHolder {

    private TextView content_tv;
    private TextView time_tv;

    public ContentHolder(View itemView) {
        super(itemView);
        content_tv = (TextView) itemView.findViewById(R.id.tv_content);
        time_tv = (TextView) itemView.findViewById(R.id.detail_time);
    }

    public void setData(Context mContext, final Complex novel) {
        if (novel.getProperties() != null && !novel.getProperties().isForbidden_right_click()) {
            ((Activity) mContext).registerForContextMenu(content_tv);
        }
        if (!TextUtils.isEmpty(novel.getContent())) {
            new AsyncTask<Void, String, String>() {
                @Override
                protected String doInBackground(Void... params) {
                    return StringUtil.htmlDecoder(novel.getContent());
                }

                @Override
                protected void onPostExecute(String s) {
                    content_tv.setText(s);
                }
            }.execute();
        }
        MyLinkfy.addLinks(content_tv, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
        UnderLineUtil.DisUnderLine(mContext, content_tv);
        if (!TextUtils.isEmpty(novel.getCtime())) {
            time_tv.setVisibility(View.VISIBLE);
            time_tv.setText(String.format(mContext.getString(R.string.post_at), StringUtil.getOppositeTime(novel.getCtime())));
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) time_tv.getLayoutParams();
            params.width = CommonUtils.getWidthPixels((Activity) mContext);
            time_tv.setLayoutParams(params);
        }
    }
}
